package top.ooyyh.util;

import java.util.List;

/**
 * 终端UI工具类，用于绘制美观的界面
 */
public class TerminalUI {
    // ANSI颜色代码
    public static final String RESET = "\033[0m";
    public static final String BOLD = "\033[1m";
    public static final String DIM = "\033[2m";
    
    // 前景色
    public static final String BLACK = "\033[30m";
    public static final String RED = "\033[31m";
    public static final String GREEN = "\033[32m";
    public static final String YELLOW = "\033[33m";
    public static final String BLUE = "\033[34m";
    public static final String MAGENTA = "\033[35m";
    public static final String CYAN = "\033[36m";
    public static final String WHITE = "\033[37m";
    
    // 背景色
    public static final String BG_BLACK = "\033[40m";
    public static final String BG_RED = "\033[41m";
    public static final String BG_GREEN = "\033[42m";
    public static final String BG_YELLOW = "\033[43m";
    public static final String BG_BLUE = "\033[44m";
    public static final String BG_MAGENTA = "\033[45m";
    public static final String BG_CYAN = "\033[46m";
    public static final String BG_WHITE = "\033[47m";
    
    // 装饰字符
    private static final String STAR = "✦";
    private static final String DOT = "•";
    private static final String ARROW = "➜";
    private static final String CHECK = "✓";
    private static final String CROSS = "✗";
    private static final String INFO = "ℹ";
    private static final String WARN = "⚠";

    /**
     * 清屏
     */
    public static void clearScreen() {
        try {
            String os = System.getProperty("os.name").toLowerCase();
            if (os.contains("windows")) {
                new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
            } else {
                System.out.print("\033[H\033[2J");
                System.out.flush();
            }
        } catch (Exception e) {
            // 如果清屏失败，至少打印一些空行
            for (int i = 0; i < 50; i++) {
                System.out.println();
            }
        }
    }

    /**
     * 打印美观的标题
     */
    public static void printTitle(String title) {
        System.out.println();
        String stars = repeat(STAR, 3);
        String line = repeat("═", 60);
        System.out.println(CYAN + BOLD + line + RESET);
        System.out.println(CYAN + BOLD + stars + "  " + title + "  " + stars + RESET);
        System.out.println(CYAN + BOLD + line + RESET);
        System.out.println();
    }

    /**
     * 打印美观的文本块（带缩进和颜色）
     */
    public static void printBox(String content) {
        String[] lines = content.split("\n");
        for (String line : lines) {
            System.out.println("  " + CYAN + DOT + RESET + " " + line);
        }
        System.out.println();
    }

    /**
     * 打印美观的菜单选项
     */
    public static void printMenu(String title, List<String> options) {
        printTitle(title);
        for (int i = 0; i < options.size(); i++) {
            String number = String.format("%2d", i + 1);
            System.out.println("  " + YELLOW + BOLD + number + RESET + " " + 
                             CYAN + ARROW + RESET + " " + options.get(i));
        }
        System.out.println();
    }

    /**
     * 打印美观的分隔线
     */
    public static void printSeparator() {
        System.out.println(DIM + repeat("─", 60) + RESET);
        System.out.println();
    }

    /**
     * 打印提示信息（带颜色和图标）
     */
    public static void printInfo(String message) {
        System.out.println("  " + BLUE + BOLD + INFO + RESET + " " + BLUE + message + RESET);
        System.out.println();
    }

    /**
     * 打印成功信息（带颜色和图标）
     */
    public static void printSuccess(String message) {
        System.out.println("  " + GREEN + BOLD + CHECK + RESET + " " + GREEN + message + RESET);
        System.out.println();
    }

    /**
     * 打印错误信息（带颜色和图标）
     */
    public static void printError(String message) {
        System.out.println("  " + RED + BOLD + CROSS + RESET + " " + RED + message + RESET);
        System.out.println();
    }

    /**
     * 打印警告信息（带颜色和图标）
     */
    public static void printWarning(String message) {
        System.out.println("  " + YELLOW + BOLD + WARN + RESET + " " + YELLOW + message + RESET);
        System.out.println();
    }

    /**
     * 重复字符串
     */
    private static String repeat(String str, int count) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < count; i++) {
            sb.append(str);
        }
        return sb.toString();
    }

    /**
     * 等待用户输入（按回车继续）
     */
    public static void waitForEnter() {
        System.out.print(DIM + "按回车键继续..." + RESET);
        try {
            System.in.read();
        } catch (Exception e) {
            // 忽略
        }
    }
    
    /**
     * 打印用户信息卡片
     */
    public static void printUserCard(String username, String role) {
        System.out.println();
        System.out.println(CYAN + BOLD + "┌" + repeat("─", 40) + "┐" + RESET);
        System.out.println(CYAN + BOLD + "│" + RESET + "  用户信息" + 
                          repeat(" ", 30) + CYAN + BOLD + "│" + RESET);
        System.out.println(CYAN + BOLD + "├" + repeat("─", 40) + "┤" + RESET);
        System.out.println(CYAN + BOLD + "│" + RESET + "  用户名: " + 
                          YELLOW + username + RESET + 
                          repeat(" ", 40 - 12 - username.length()) + CYAN + BOLD + "│" + RESET);
        System.out.println(CYAN + BOLD + "│" + RESET + "  角色:   " + 
                          MAGENTA + role + RESET + 
                          repeat(" ", 40 - 12 - role.length()) + CYAN + BOLD + "│" + RESET);
        System.out.println(CYAN + BOLD + "└" + repeat("─", 40) + "┘" + RESET);
        System.out.println();
    }
    
    /**
     * 打印欢迎横幅
     */
    public static void printWelcomeBanner() {
        System.out.println();
        System.out.println(CYAN + BOLD + 
            "╔═══════════════════════════════════════════════════════╗" + RESET);
        System.out.println(CYAN + BOLD + 
            "║" + RESET + 
            repeat(" ", 20) +
            YELLOW + BOLD + "✨ 任务管理系统 ✨" + RESET +
            repeat(" ", 19) +
            CYAN + BOLD + "║" + RESET);
        System.out.println(CYAN + BOLD + 
            "╚═══════════════════════════════════════════════════════╝" + RESET);
        System.out.println();
    }
}

